package com.kedacom.demo.redis.jedis;

import java.util.Date;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;

/**
 * RedisTest
 * @author luocanfeng
 * @date 2015年8月5日
 */
public class RedisTest {

	private static Logger logger = LoggerFactory.getLogger(RedisTest.class);

	public static void main(String[] args) throws InterruptedException {
		simpleTest();
		// performanceTest();
		// scriptTest();
		testPing();
	}

	public static void testPing() throws InterruptedException {
		// Jedis jedis = new Jedis("172.16.80.113", 6379);
		for (int i = 0; i < 10000000; i++) {
			try {
				Jedis jedis = RedisUtils.getJedis();
				System.out.println(jedis.ping() + ", " + new Date().toGMTString());
				RedisUtils.returnJedis(jedis);
			} catch (Exception e) {
				e.printStackTrace();
			}
			Thread.sleep(500);
		}
		// jedis.close();
	}

	/**
	 * 简单命令测试
	 */
	public static void simpleTest() {
		Jedis jedis = RedisUtils.getJedis();
		jedis.set("key", "value");
		Set<String> keys = jedis.keys("*");
		logger.debug("key size={}", keys.size());
		for (String key : keys) {
			logger.debug("key={}", key);
		}
		jedis.del("key");
		RedisUtils.returnJedis(jedis);
	}

	/**
	 * 性能测试
	 */
	public static void performanceTest() {
		int taskNum = 200;
		int taskRunTimes = 5000;

		// taskNum = 10;
		// taskRunTimes = 10;

		for (int i = 1; i <= taskNum; i++) {
			new Thread(new SimpleStringWriteTask("SimpleStringWriteTask_" + i, taskRunTimes)).start();
		}
	}

	/**
	 * lua脚本测试
	 */
	public static void scriptTest() {

	}

}
